// pages/test/test.js
Page({
  //获取系统信息,当前系统属于哪一个版本，属于哪个平台
  getSystemInfo() {
    let that = this;
    wx.getSystemInfo({
      success: function(res) {
        console.log('getSystemInfo is successful')
        that.setData({
          "device.model": res.model
        })
        that.setData({
          "device.version": res.version
        })
        if (that.data.device.model.indexOf('iPhone') !== -1) {
          that.setData({
            "device.isIphone": true
          })
        }
        that.startWifi();
      }
    })

  },
  //初始化wifi对象
  startWifi() {
    let that = this;
    if (wx.canIUse('startWifi')) {
      wx.startWifi({
        complete: function(res) {
          if (res.errCode === 0 || res.errMsg === "startWifi:ok") {
            console.log("startWifi:ok")
            that.getWifiList();
          } else {
            wx.showToast({
              title: res.errCode + ": " + res.errMsg,
              icon: 'none'
            })
          }
        }
      })
    } else {
      console.log('can not use start wifi')
    }
  },
  //获取wifiList列表
  getWifiList() {
    let that = this;
    wx.getWifiList({
      success: function(res) {

      },
      error: function(res) {

      },
      complete: function(res) {
        console.log(res);
        if (res.errCode === 0 || res.errCode === "getWifiList: ok") {
          console.log('getWifiList: ok');
        } else {
          wx.showToast({
            title: res.errCode + ": " + res.errMsg,
            icon: 'none'
          })
        }
      }
    });
    wx.onGetWifiList(function(res) {
      if (res.wifiList) {
        that.setData({
          "wifiList": res.wifiList
        });
      }
    })
  },
  //连接wifi
  connectWifi() {
    let wifiList = this.data.wifiList;

    let activeIndex = this.data.activeIndex;
    this.setData({
      "wifi.SSID": wifiList[activeIndex].SSID
    })
    this.setData({
      "wifi.BSSID": wifiList[activeIndex].BSSID
    })
    let wifi = this.data.wifi;
    if (this.data.device.isIphone) {
      wx.setWifiList({
        wifiList: [{
          SSID: wifi.SSID,
          BSSID: wifi.BSSID,
          password: wifi.password
        }],
        complete: function(res) {
          console.log(res);
          if (res.errCode === 0) {

          } else {
            wx.showToast({
              title: res.errCode + ": " + res.errMsg,
              icon: 'none'
            })
          }
        }
      })
    } 
    wx.connectWifi({
      SSID: wifi.SSID,
      BSSID: wifi.BSSID,
      password: wifi.password,
      complete: function (res) {
        if (res.errCode === 0) {

        } else {
          wx.showToast({
            title: res.errCode + ": " + res.errMsg,
            icon: 'none'
          })
        }
      }
    })
    wx.onWifiConnected(function(res){
      console.log(res)
    })
  },
  //用户确定已经打开wifi
  openWifi() {
    this.startWifi();
  },
  chooseWifi(e) {
    console.log(e)
    let activeIndex = e.currentTarget.dataset.index;
    this.setData({
      "activeIndex": activeIndex
    })
    this.connectWifi();
  },
  errorDeal(res){
    wx.showToast({
      title: res.errCode + ": " + res.errMsg,
      icon: 'none'
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    device: {
      model: '',
      isIphone: false,
      version: ''
    },
    wifi: {
      "SSID": 'PuGongYing',
      "BSSID": "d4:68:ba:86:63:54",
      "password": '',
    },
    wifiList: [


    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getSystemInfo();
    let that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})